import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */
import componentsRouter from './modules/components'
import tableRouter from './modules/table'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    key:'Z',
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    key:'Z',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    key:'Z',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    key:'A',
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        key:'A1',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/profile',
    key:'Z',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: 'Profile', icon: 'user', noCache: true }
      }
    ]
  }
]

export const asyncRoutes = [
  // {
  //   path: '/permission',
  //   component: Layout,
  //   key:'B',
  //   redirect: '/permission/page',
  //   alwaysShow: true, // will always show the root menu
  //   name: 'Permission',
  //   meta: {
  //     title: '权限管理',
  //     icon: 'lock',
  //     roles: ['admin', 'editor'] // you can set roles in root nav
  //   },
  //   children: [
  //     {
  //       path: 'role',
  //       key:'B1',
  //       component: () => import('@/views/permission/role'),
  //       name: 'RolePermission',
  //       meta: {
  //         title: '权限管理',
  //         roles: ['admin']
  //       }
  //     }
  //   ]
  // },

  {
    path: '/usrgroup',
    key:'G',
    component: Layout,
    redirect: '/usrgroup/list',
    name: 'Usrgroup',
    meta: {
      title: '用户组管理',
      icon: 'lock'
    },
    children: [
      {
        path: 'create',
        key:'G1',
        component: () => import('@/views/usrgroup/create'),
        name: 'CreateUsrgroup',
        meta: { title: '用户组新增', icon: 'edit' },
        hidden: true
      },
      {
        path: 'edit/:id(\\d+)',
        key:'G2',
        component: () => import('@/views/usrgroup/edit'),
        name: 'EditUsrgroup',
        meta: { title: '用户组编辑', noCache: true, activeMenu: '/usrgroup/list' },
        hidden: true
      },
      {
        path: 'list',
        key:'G3',
        component: () => import('@/views/usrgroup/list'),
        name: 'UsrgroupList',
        meta: { title: '用户组清单', icon: 'lock' }
      }
    ]
  },

  {
    path: '/usr',
    key:'C',
    component: Layout,
    redirect: '/usr/list',
    name: 'Usr',
    meta: {
      title: '用户管理',
      icon: 'el-icon-user-solid'
    },
    children: [
      {
        path: 'create',
        key:'C1',
        component: () => import('@/views/usr/create'),
        name: 'CreateUsr',
        meta: { title: '用户新增', icon: 'edit' }
      },
      {
        path: 'edit/:id(\\d+)',
        key:'C2',
        component: () => import('@/views/usr/edit'),
        name: 'EditUsr',
        meta: { title: '用户编辑', noCache: true, activeMenu: '/usr/list' },
        hidden: true
      },
      {
        path: 'list',
        key:'C3',
        component: () => import('@/views/usr/list'),
        name: 'UsrList',
        meta: { title: '用户清单', icon: 'list' }
      }
    ]
  },

  {
    path: '/mpcp',
    key:'D',
    component: Layout,
    redirect: '/mpcp/list',
    name: 'MPCP',
    meta: {
      title: 'LOT执行管理',
      icon: 'el-icon-s-order'
    },
    children: [
      {
        path: 'create',
        key:'D1',
        component: () => import('@/views/mpcp/create'),
        name: 'CreateMPCP',
        meta: { title: 'LOT执行新增', icon: 'edit' },
        hidden: true
      },
      {
        path: 'edit/:id(\\d+)',
        key:'D2',
        component: () => import('@/views/mpcp/edit'),
        name: 'EditMPCP',
        meta: { title: 'LOT执行更新', noCache: true, activeMenu: '/mpcp/list' },
        hidden: true
      },
      {
        path: 'list',
        key:'D3',
        component: () => import('@/views/mpcp/list'),
        name: 'MPCPList',
        meta: { title: 'LOT执行履历', icon: 'el-icon-s-order' }
      }
    ]
  },

  {
    path: '/mpcpend',
    key:'I',
    component: Layout,
    redirect: '/mpcpend/list',
    name: 'MPCPend',
    meta: {
      title: '生产/质量数据管理',
      icon: 'el-icon-s-help'
    },
    children: [
      {
        path: 'create',
        key:'I1',
        component: () => import('@/views/mpcpend/create'),
        name: 'CreateMPCPend',
        meta: { title: '生产/质量数据新增', icon: 'edit' },
        hidden: true
      },
      {
        path: 'edit/:id(\\d+)',
        key:'I2',
        component: () => import('@/views/mpcpend/edit'),
        name: 'EditMPCPend',
        meta: { title: '生产/质量数据更新', noCache: true, activeMenu: '/mpcpend/list' },
        hidden: true
      },
      {
        path: 'list',
        key:'I3',
        component: () => import('@/views/mpcpend/list'),
        name: 'MPCPendList',
        meta: { title: '生产/质量数据清单', icon: 'list' }
      }
    ]
  },


  {
    path: '/boudf',
    key:'H',
    component: Layout,
    redirect: '/boudf/list',
    name: 'BOUDF',
    meta: {
      title: 'LOT数据管理',
      icon: 'el-icon-s-marketing'
    },
    children: [
      {
        path: 'create',
        key:'H1',
        component: () => import('@/views/boudf/create'),
        name: 'CreateBOUDF',
        meta: { title: 'LOT数据新增', icon: 'edit' },
        hidden: true
      },
      {
        path: 'edit/:id(\\d+)',
        key:'H2',
        component: () => import('@/views/boudf/edit'),
        name: 'EditBOUDF',
        meta: { title: 'LOT数据更新', noCache: true, activeMenu: '/boudf/list' },
        hidden: true
      },
      {
        path: 'list',
        key:'H3',
        component: () => import('@/views/boudf/list'),
        name: 'BOUDFList',
        meta: { title: 'LOT数据履历', icon: 'el-icon-s-marketing' }
      }
    ]
  },

  {
    path: '/res',
    key:'E',
    component: Layout,
    redirect: '/res/list',
    name: 'Res',
    meta: {
      title: '设备管理',
      icon: 'el-icon-s-platform'
    },
    children: [
      {
        path: 'create',
        key:'E1',
        component: () => import('@/views/res/create'),
        name: 'CreateRes',
        meta: { title: '设备新增', icon: 'edit' },
        hidden: true
      },
      {
        path: 'edit/:id(\\d+)',
        key:'E2',
        component: () => import('@/views/res/edit'),
        name: 'EditRes',
        meta: { title: '设备编辑', noCache: true, activeMenu: '/res/list' },
        hidden: true
      },
      {
        path: 'list',
        key:'E3',
        component: () => import('@/views/res/list'),
        name: 'ResList',
        meta: { title: '设备清单', icon: 'el-icon-s-platform' }
      }
    ]
  },

  {
    path: '/edc',
    key:'F',
    component: Layout,
    redirect: '/edc/list',
    name: 'EDC',
    meta: {
      title: '参数管理',
      icon: 'el-icon-s-tools'
    },
    children: [
      {
        path: 'create',
        key:'F1',
        component: () => import('@/views/edc/create'),
        name: 'CreateEDC',
        meta: { title: 'Create EDC', icon: 'edit' },
        hidden: true
      },
      {
        path: 'edit/:id(\\d+)',
        key:'F2',
        component: () => import('@/views/edc/edit'),
        name: 'EditEDC',
        meta: { title: '参数编辑', noCache: true, activeMenu: '/edc/list' },
        hidden: true
      },
      {
        path: 'list',
        key:'F3',
        component: () => import('@/views/edc/list'),
        name: 'EDCList',
        meta: { title: '参数管理', icon: 'el-icon-s-tools' }
      }
    ]
  },

  {
    path: '/dropdown',
    key: 'J',
    component: Layout,
    redirect: '/dropdown/list',
    name: 'Dropdown',
    meta: {
      title: '自定义参数管理',
      icon: 'el-icon-s-tools',
    },
    children: [
      {
        path: 'create',
        component: () => import('@/views/dropdown/create'),
        key: 'J1',
        name: 'CreateDropdown',
        meta: { title: '参数新增', icon: 'edit', roles: ['admin'] },
        hidden: true
      },
      {
        path: 'edit/:id(\\d+)',
        component: () => import('@/views/dropdown/edit'),
        key: 'J2',
        name: 'EditDropdown',
        meta: { title: '参数编辑', noCache: true },
        hidden: true
      },
      {
        path: 'list',
        component: () => import('@/views/dropdown/list'),
        key: 'J3',
        name: 'DropdownList',
        meta: { title: '自定义参数管理', icon: 'el-icon-s-tools' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
